package cn.wildfirechat.message;

import android.os.Parcel;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.wildfirechat.message.core.ContentTag;
import cn.wildfirechat.message.core.MessagePayload;
import cn.wildfirechat.message.core.PersistFlag;

import static cn.wildfirechat.message.core.MessageContentType.ANNOUNCEMENT_CHAT;

/**
 * 公告
 */

@ContentTag(type = ANNOUNCEMENT_CHAT, flag = PersistFlag.Persist_And_Count)
public class AnnouncementContent extends MessageContent {

    private String cover;//封面图片链接
    private String title;//标题
    private String articleLink;//文章链接
    private String searchableContent;

    public String getCover() {
        return cover;
    }

    public void setCover(String cover) {
        this.cover = cover;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getArticleLink() {
        return articleLink;
    }

    public void setArticleLink(String articleLink) {
        this.articleLink = articleLink;
    }


    public AnnouncementContent() {
    }

    public AnnouncementContent(String cover, String title, String articleLink) {
        this.cover = cover;
        this.title = title;
        this.articleLink = articleLink;
    }

    @Override
    public MessagePayload encode() {
        MessagePayload messagePayload = new MessagePayload();
        try {
            JSONObject objWrite = new JSONObject();
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("cover", cover);
            jsonObject.put("title", title);
            jsonObject.put("articleLink", articleLink);
            objWrite.put("notice", jsonObject);
            messagePayload.content = objWrite.toString();
            messagePayload.searchableContent = searchableContent;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return messagePayload;
    }

    @Override
    public void decode(MessagePayload payload) {
        if (!TextUtils.isEmpty(payload.content)) {
            try {
                if (payload.content != null) {
                    JSONObject jsonObject = new JSONObject(payload.content);
                    JSONObject notice = jsonObject.getJSONObject("notice");
                    cover = notice.optString("cover");
                    title = notice.optString("title");
                    articleLink = notice.optString("articleLink");
                    searchableContent = payload.searchableContent;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    protected AnnouncementContent(Parcel in) {
        super(in);
        this.cover = in.readString();
        this.title = in.readString();
        this.articleLink = in.readString();
        this.searchableContent = in.readString();
    }


    @Override
    public String digest(Message message) {
        return searchableContent;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(this.cover);
        dest.writeString(this.title);
        dest.writeString(this.articleLink);
        dest.writeString(this.searchableContent);

    }

    public static final Creator<AnnouncementContent> CREATOR = new Creator<AnnouncementContent>() {
        @Override
        public AnnouncementContent createFromParcel(Parcel source) {
            return new AnnouncementContent(source);
        }

        @Override
        public AnnouncementContent[] newArray(int size) {
            return new AnnouncementContent[size];
        }
    };
}
